﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;
using System.Web;
using Ext.Net;

namespace SalesManagement.Controls.Sales
{
    public partial class RoleForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {            
            this.winDetails.Show();
        }
        public void Show(string roleId)
        {
            RoleEntity roleE = new RoleEntity();
            RoleService roleS = RoleService.GetInstance();
            roleE.RoleId = int.Parse(roleId);
            roleE = roleS.LoadByPrimaryKey(roleE);
            if (null == roleE)
                return;
            hdfRoleId.Value = roleE.RoleId;
            txtRoleName.Text = roleE.RoleName;
            txtDescription.Text = roleE.Description;
            ckbIsActive.Checked = roleE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            RoleEntity roleE = new RoleEntity();
            RoleService roleS = RoleService.GetInstance();
            if ("" != hdfRoleId.Value.ToString())
            {
                string roleId = hdfRoleId.Value.ToString();
                roleE.RoleId = int.Parse(roleId);
                roleE = roleS.LoadByPrimaryKey(roleE);
                roleE.RoleName = txtRoleName.Text;
                roleE.Description = txtDescription.Text;
                roleE.IsActive = ckbIsActive.Checked;
                roleE.UpdatedDate = DateTime.Now;
                roleE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                roleS.Update(roleE);
            }
            else
            {
                roleE.RoleName = txtRoleName.Text;
                roleE.Description = txtDescription.Text;
                roleE.IsActive = ckbIsActive.Checked;
                roleE.CreatedDate = DateTime.Now;
                roleE.CreatedBy = FSSContext.Current.User.EmployeeId;
                roleE.UpdatedDate = DateTime.Now;
                roleE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                roleS.Insert(roleE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}